package fmod;

import zombie.debug.DebugLog;

/* loaded from: SoundBuffer.class */
public class SoundBuffer {
    public int Buf_Size;
    private short[] intdata;
    public int Buf_Read = 0;
    public int Buf_Write = 0;
    private int delay = 1;

    public SoundBuffer(int i) {
        this.Buf_Size = i;
        this.intdata = new short[i];
    }

    public void get(long j, short[] sArr) {
        int i;
        int i2 = this.Buf_Write - this.Buf_Read;
        if (i2 < 0) {
            i2 += this.Buf_Size;
        }
        if (i2 < j) {
            for (int i3 = 0; i3 < j - 1; i3++) {
                sArr[i3] = 0;
            }
            return;
        }
        if (i2 > j * this.delay * 2) {
            if (this.delay * j * 3 < this.Buf_Size) {
                this.delay++;
            }
            DebugLog.log("[SoundBuffer] correct: delay: " + this.delay);
            this.Buf_Read = (int) (this.Buf_Write - (j * this.delay));
            if (this.Buf_Read < 0) {
                this.Buf_Read += this.Buf_Size;
            }
            int i4 = 0;
            int i5 = this.Buf_Read;
            while (true) {
                int i6 = i5;
                if (i4 < j * 2) {
                    this.intdata[i6] = 0;
                    i4++;
                    i5 = (i6 + 1) % this.Buf_Size;
                } else {
                    return;
                }
            }
        } else {
            int i7 = 0;
            int i8 = this.Buf_Read;
            while (true) {
                i = i8;
                if (i7 >= j - 1 || i == this.Buf_Write) {
                    break;
                }
                sArr[i7] = this.intdata[i];
                i7++;
                i8 = (i + 1) % this.Buf_Size;
            }
            this.Buf_Read = i;
        }
    }

    public void push(long j, short[] sArr) {
        int i;
        boolean z = false;
        int i2 = 0;
        int i3 = this.Buf_Write;
        while (true) {
            i = i3;
            if (i2 >= j - 1) {
                break;
            }
            this.intdata[i] = sArr[i2];
            if (sArr[i2] != 0) {
                z = true;
            }
            i2++;
            i3 = (i + 1) % this.Buf_Size;
        }
        if (z) {
            this.Buf_Write = i;
        }
    }

    public void push(long j, byte[] bArr) {
        int i;
        boolean z = false;
        int i2 = 0;
        int i3 = this.Buf_Write;
        while (true) {
            i = i3;
            if (i2 >= j - 1) {
                break;
            }
            this.intdata[i] = (short) ((bArr[i2 + 1] * 256) + bArr[i2]);
            if (bArr[i2] != 0) {
                z = true;
            }
            i2 += 2;
            i3 = (i + 1) % this.Buf_Size;
        }
        if (z) {
            this.Buf_Write = i;
        }
    }

    public short[] buf() {
        return this.intdata;
    }
}